package swingxrcp.framework.client.context;

import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;

public class SwingXContext {
	// TODO SwingX框架启动属性配置====================================================
	/** *** 启动文件 *** */
	public static String SWINGX_CONFIG_FILE = "";

	/** *** 主框架类 *** */
	public static String MAIN_FRAME_CLASS = " Not Define Main Window Class ";

	/** *** 数据源路径 *** */
	public static String DATA_SOURCE;

	/** *** 检验模型 *** */
	public static String VALIDATION_MODEL;
	// TODO Space Model 类型==================================================
	/** *** 空间模型路径 *** */
	public static String SPACE_MODEL_PATH = "";

	public static final String DB_SPACE_MODEL = "DB";

	public static final String DOMAIN_SPACE_MODEL = "domain";

	// TODO 缓存接口==================================================
	private static ConcurrentHashMap<Object, Object> cacheMap = new ConcurrentHashMap<Object, Object>(100, 0.75f, 100);

	/**
	 * get cache from cache map
	 * 
	 * @param id
	 *            class name
	 * @param key
	 *            key of the cache
	 * @return
	 */
	public static Object get(String id, String key) {
		String fullKey = id +"_" +key;
		return cacheMap.get(fullKey);
	}

	public static void add(String id, String key, Object object) {
		String fullKey = id +"_" +key;
		cacheMap.put(fullKey, object);
	}

	public static void add(String id, String key, Object object, boolean lock) {
		add(id, key, object);
	}

	public static void remove(String id, String key) {
		String fullKey = id +"_" +key;
		cacheMap.remove(fullKey);
	}

	/**
	 * remove the whole cache which created by a specify entity
	 * 
	 * @param clazzName
	 *            should be the name of class
	 */
	public static void removeCacheById(String clazzName) {
		for (Enumeration iterator = cacheMap.keys(); iterator.hasMoreElements();) {
			String key = (String) iterator.nextElement();
			if (key ==null)
				continue;
			if (key.startsWith(clazzName +"_")) {
				cacheMap.remove(key);
			}
		}
	}
}
